/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cloud.edu.sys.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.edu.sys.model.Captcha;
import com.cloud.edu.sys.service.SysCaptchaService;
import com.google.code.kaptcha.Producer;

import java.awt.image.BufferedImage;

/**
 * 验证码
 *
 * @since 2.0.0 2018-02-10
 */
@Service("sysCaptchaService1")
public class SysCaptchaServiceImpl implements SysCaptchaService {
	
    @Autowired
    private Producer producer;

    @Override
    public BufferedImage getCaptcha(String uuid) throws Exception {
        if(StringUtils.isBlank(uuid)){
            throw new Exception("uuid不能为空");
        }
        //生成文字验证码
        String code = producer.createText();

        Captcha captcha = new Captcha();
        captcha.setUuid(uuid);
        captcha.setCode(code);
        //5分钟后过期
//        captcha.setExpireTime(DateUtils.addDateMinutes(new Date(), 5));
//        this.insert(captchaEntity);

        return producer.createImage(code);
    }

    /**
     * 验证码验证
     */
    @Override
    public boolean validate(String uuid, String code) {
//        SysCaptchaEntity captchaEntity = this.selectOne(new EntityWrapper<SysCaptchaEntity>().eq("uuid", uuid));
//        if(captchaEntity == null){
//            return false;
//        }
//
//        //删除验证码
//        this.deleteById(uuid);
//
//        if(captchaEntity.getCode().equalsIgnoreCase(code) && captchaEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
//            return true;
//        }

        return true;
    }
}
